﻿namespace QLPhongKham.UC
{
    using ConfigDatabase.Class;
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraTab;
    using DevExpress.XtraTreeList;
    using DevExpress.XtraTreeList.Columns;
    using DevExpress.XtraTreeList.Nodes;
    using QLPhongKham.BaseClass.Chung;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class UCphanquyensudung : XtraUserControl
    {
        private ConnectDatabase _condata;
        private DataTable _dataChucNang;
        private SimpleButton btnketthuc;
        private SimpleButton btnluu;
        private IContainer components = null;
        private RepositoryItemCheckEdit repositoryItemCheckEdit1;
        private TreeList treeList1;
        private TreeListColumn treeListColumn1;
        private TreeListColumn treeListColumn4;

        public UCphanquyensudung()
        {
            this.InitializeComponent();
        }

        private void btnketthuc_Click(object sender, EventArgs e)
        {
            try
            {
                XtraTabPage parent = (XtraTabPage) base.Parent;
                XtraTabControl control = (XtraTabControl) parent.Parent;
                control.TabPages.Remove(parent);
            }
            catch
            {
                XtraUserControl control2 = (XtraUserControl) base.Parent;
                control2.Controls.Remove(this);
            }
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            string quyensudung = "";
            foreach (TreeListNode node in this.treeList1.Nodes)
            {
                if (node.Checked)
                {
                    quyensudung = quyensudung + node.GetValue("ma") + ",";
                }
                if (node.Nodes.Count > 0)
                {
                    quyensudung = quyensudung + this.f_getNodeChild_check(node);
                }
            }
            string[] strArray = quyensudung.Trim(new char[] { ',' }).Split(new char[] { ',' });
            quyensudung = "";
            for (int i = 0; i < strArray.Length; i++)
            {
                if (quyensudung.IndexOf(strArray[i] + ",") == -1)
                {
                    quyensudung = quyensudung + strArray[i] + ",";
                }
            }
            quyensudung = quyensudung.Trim(new char[] { ',' });
            dtoDSThongSo so = new dtoDSThongSo(this._condata);
            if (!so.TLQuyenSuDung(quyensudung, 1))
            {
                MessageBox.Show("Lưu danh s\x00e1ch c\x00e1c chức năng kh\x00f4ng th\x00e0nh c\x00f4ng.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                MessageBox.Show("Lưu th\x00e0nh c\x00f4ng.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void f_checknode()
        {
            string key = new dtoDSThongSo(this._condata).TVQuyenSuDung(1);
            foreach (TreeListNode node in this.treeList1.Nodes)
            {
                node.Checked = key.IndexOf(node.GetValue(0).ToString() + ",") > -1;
                if (node.HasChildren)
                {
                    this.f_searchnode(node, key);
                }
            }
        }

        private string f_getNodeChild_check(TreeListNode tln)
        {
            string str = "";
            foreach (TreeListNode node in tln.Nodes)
            {
                if (node.Checked)
                {
                    str = str + node.GetValue("ma") + ",";
                }
                if (node.Nodes.Count > 0)
                {
                    str = str + this.f_getNodeChild_check(node);
                }
            }
            return str;
        }

        private void f_searchnode(TreeListNode _node, string key)
        {
            _node.Checked = key.IndexOf(_node.GetValue("ma") + ",") > -1;
            if (_node.HasChildren)
            {
                foreach (TreeListNode node in _node.Nodes)
                {
                    _node.Checked = key.IndexOf(node.GetValue("ma") + ",") > -1;
                    if (node.HasChildren)
                    {
                        this.f_searchnode(node, key);
                    }
                }
            }
        }

        private void f_setNodeChild_check(TreeListNode tln)
        {
            foreach (TreeListNode node in tln.Nodes)
            {
                node.Checked = tln.Checked;
                if (node.Nodes.Count > 0)
                {
                    this.f_setNodeChild_check(node);
                }
            }
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(UCphanquyensudung));
            this.treeList1 = new TreeList();
            this.treeListColumn1 = new TreeListColumn();
            this.treeListColumn4 = new TreeListColumn();
            this.repositoryItemCheckEdit1 = new RepositoryItemCheckEdit();
            this.btnketthuc = new SimpleButton();
            this.btnluu = new SimpleButton();
            this.treeList1.BeginInit();
            this.repositoryItemCheckEdit1.BeginInit();
            base.SuspendLayout();
            this.treeList1.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.treeList1.Appearance.Empty.BackColor = Color.FromArgb(0xf3, 0xf3, 0xf3);
            this.treeList1.Appearance.Empty.Options.UseBackColor = true;
            this.treeList1.Appearance.EvenRow.BackColor = Color.FromArgb(0xdf, 0xdf, 0xdf);
            this.treeList1.Appearance.EvenRow.BackColor2 = Color.GhostWhite;
            this.treeList1.Appearance.EvenRow.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.EvenRow.ForeColor = Color.Black;
            this.treeList1.Appearance.EvenRow.GradientMode = LinearGradientMode.ForwardDiagonal;
            this.treeList1.Appearance.EvenRow.Options.UseBackColor = true;
            this.treeList1.Appearance.EvenRow.Options.UseFont = true;
            this.treeList1.Appearance.EvenRow.Options.UseForeColor = true;
            this.treeList1.Appearance.FocusedCell.BackColor = Color.FromArgb(0xff, 0xff, 0xe1);
            this.treeList1.Appearance.FocusedCell.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.FocusedCell.ForeColor = Color.Black;
            this.treeList1.Appearance.FocusedCell.Options.UseBackColor = true;
            this.treeList1.Appearance.FocusedCell.Options.UseFont = true;
            this.treeList1.Appearance.FocusedCell.Options.UseForeColor = true;
            this.treeList1.Appearance.FocusedRow.BackColor = Color.Navy;
            this.treeList1.Appearance.FocusedRow.BackColor2 = Color.FromArgb(50, 50, 0xb2);
            this.treeList1.Appearance.FocusedRow.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.FocusedRow.ForeColor = Color.White;
            this.treeList1.Appearance.FocusedRow.Options.UseBackColor = true;
            this.treeList1.Appearance.FocusedRow.Options.UseFont = true;
            this.treeList1.Appearance.FocusedRow.Options.UseForeColor = true;
            this.treeList1.Appearance.FooterPanel.BackColor = Color.Silver;
            this.treeList1.Appearance.FooterPanel.BorderColor = Color.Silver;
            this.treeList1.Appearance.FooterPanel.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.FooterPanel.ForeColor = Color.Black;
            this.treeList1.Appearance.FooterPanel.Options.UseBackColor = true;
            this.treeList1.Appearance.FooterPanel.Options.UseBorderColor = true;
            this.treeList1.Appearance.FooterPanel.Options.UseFont = true;
            this.treeList1.Appearance.FooterPanel.Options.UseForeColor = true;
            this.treeList1.Appearance.GroupButton.BackColor = Color.Silver;
            this.treeList1.Appearance.GroupButton.BorderColor = Color.Silver;
            this.treeList1.Appearance.GroupButton.ForeColor = Color.Black;
            this.treeList1.Appearance.GroupButton.Options.UseBackColor = true;
            this.treeList1.Appearance.GroupButton.Options.UseBorderColor = true;
            this.treeList1.Appearance.GroupButton.Options.UseForeColor = true;
            this.treeList1.Appearance.GroupFooter.BackColor = Color.FromArgb(0xca, 0xca, 0xca);
            this.treeList1.Appearance.GroupFooter.BorderColor = Color.FromArgb(0xca, 0xca, 0xca);
            this.treeList1.Appearance.GroupFooter.ForeColor = Color.Black;
            this.treeList1.Appearance.GroupFooter.Options.UseBackColor = true;
            this.treeList1.Appearance.GroupFooter.Options.UseBorderColor = true;
            this.treeList1.Appearance.GroupFooter.Options.UseForeColor = true;
            this.treeList1.Appearance.HeaderPanel.BackColor = Color.Silver;
            this.treeList1.Appearance.HeaderPanel.BorderColor = Color.Silver;
            this.treeList1.Appearance.HeaderPanel.Font = new Font("Tahoma", 10f, FontStyle.Bold);
            this.treeList1.Appearance.HeaderPanel.ForeColor = Color.Black;
            this.treeList1.Appearance.HeaderPanel.Options.UseBackColor = true;
            this.treeList1.Appearance.HeaderPanel.Options.UseBorderColor = true;
            this.treeList1.Appearance.HeaderPanel.Options.UseFont = true;
            this.treeList1.Appearance.HeaderPanel.Options.UseForeColor = true;
            this.treeList1.Appearance.HideSelectionRow.BackColor = Color.Gray;
            this.treeList1.Appearance.HideSelectionRow.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.HideSelectionRow.ForeColor = Color.FromArgb(0xd4, 0xd0, 200);
            this.treeList1.Appearance.HideSelectionRow.Options.UseBackColor = true;
            this.treeList1.Appearance.HideSelectionRow.Options.UseFont = true;
            this.treeList1.Appearance.HideSelectionRow.Options.UseForeColor = true;
            this.treeList1.Appearance.HorzLine.BackColor = Color.Silver;
            this.treeList1.Appearance.HorzLine.Options.UseBackColor = true;
            this.treeList1.Appearance.OddRow.BackColor = Color.White;
            this.treeList1.Appearance.OddRow.BackColor2 = Color.White;
            this.treeList1.Appearance.OddRow.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.OddRow.ForeColor = Color.Black;
            this.treeList1.Appearance.OddRow.GradientMode = LinearGradientMode.BackwardDiagonal;
            this.treeList1.Appearance.OddRow.Options.UseBackColor = true;
            this.treeList1.Appearance.OddRow.Options.UseFont = true;
            this.treeList1.Appearance.OddRow.Options.UseForeColor = true;
            this.treeList1.Appearance.Preview.BackColor = Color.White;
            this.treeList1.Appearance.Preview.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.Preview.ForeColor = Color.Navy;
            this.treeList1.Appearance.Preview.Options.UseBackColor = true;
            this.treeList1.Appearance.Preview.Options.UseFont = true;
            this.treeList1.Appearance.Preview.Options.UseForeColor = true;
            this.treeList1.Appearance.Row.BackColor = Color.White;
            this.treeList1.Appearance.Row.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.Row.ForeColor = Color.Black;
            this.treeList1.Appearance.Row.Options.UseBackColor = true;
            this.treeList1.Appearance.Row.Options.UseFont = true;
            this.treeList1.Appearance.Row.Options.UseForeColor = true;
            this.treeList1.Appearance.SelectedRow.BackColor = Color.FromArgb(10, 10, 0x8a);
            this.treeList1.Appearance.SelectedRow.Font = new Font("Tahoma", 10f);
            this.treeList1.Appearance.SelectedRow.ForeColor = Color.White;
            this.treeList1.Appearance.SelectedRow.Options.UseBackColor = true;
            this.treeList1.Appearance.SelectedRow.Options.UseFont = true;
            this.treeList1.Appearance.SelectedRow.Options.UseForeColor = true;
            this.treeList1.Appearance.TreeLine.BackColor = Color.FromArgb(0x3a, 0x3a, 0x3a);
            this.treeList1.Appearance.TreeLine.Options.UseBackColor = true;
            this.treeList1.Appearance.VertLine.BackColor = Color.Silver;
            this.treeList1.Appearance.VertLine.Options.UseBackColor = true;
            this.treeList1.Columns.AddRange(new TreeListColumn[] { this.treeListColumn1, this.treeListColumn4 });
            this.treeList1.Font = new Font("Tahoma", 10f);
            this.treeList1.Location = new Point(3, 12);
            this.treeList1.Name = "treeList1";
            this.treeList1.OptionsView.EnableAppearanceEvenRow = true;
            this.treeList1.OptionsView.EnableAppearanceOddRow = true;
            this.treeList1.OptionsView.ShowCheckBoxes = true;
            this.treeList1.RepositoryItems.AddRange(new RepositoryItem[] { this.repositoryItemCheckEdit1 });
            this.treeList1.Size = new Size(0x35e, 0x1dd);
            this.treeList1.TabIndex = 1;
            this.treeList1.AfterCheckNode += new NodeEventHandler(this.treeList1_AfterCheckNode);
            this.treeList1.KeyDown += new KeyEventHandler(this.UCphanquyensudung_KeyDown);
            this.treeListColumn1.Caption = "Chức năng";
            this.treeListColumn1.FieldName = "Chức năng";
            this.treeListColumn1.MinWidth = 0x20;
            this.treeListColumn1.Name = "treeListColumn1";
            this.treeListColumn1.OptionsColumn.AllowEdit = false;
            this.treeListColumn1.OptionsColumn.ReadOnly = true;
            this.treeListColumn1.Visible = true;
            this.treeListColumn1.VisibleIndex = 0;
            this.treeListColumn1.Width = 0x187;
            this.treeListColumn4.Caption = "ma";
            this.treeListColumn4.FieldName = "ma";
            this.treeListColumn4.Name = "treeListColumn4";
            this.repositoryItemCheckEdit1.AutoHeight = false;
            this.repositoryItemCheckEdit1.Name = "repositoryItemCheckEdit1";
            this.btnketthuc.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnketthuc.Appearance.Options.UseTextOptions = true;
            this.btnketthuc.Appearance.TextOptions.HAlignment = HorzAlignment.Near;
            this.btnketthuc.Image = Properties.Resources.imgclose;// (Image)manager.GetObject("btnketthuc.Image");
            this.btnketthuc.Location = new Point(0x57, 0x1ef);
            this.btnketthuc.Name = "btnketthuc";
            this.btnketthuc.Size = new Size(0x6a, 0x17);
            this.btnketthuc.TabIndex = 20;
            this.btnketthuc.Text = " &Kết th\x00fac [F12]";
            this.btnketthuc.Click += new EventHandler(this.btnketthuc_Click);
            this.btnluu.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.btnluu.Appearance.Options.UseTextOptions = true;
            this.btnluu.Appearance.TextOptions.HAlignment = HorzAlignment.Near;
            this.btnluu.Image = Properties.Resources.imgsave;// (Image)manager.GetObject("btnluu.Image");
            this.btnluu.Location = new Point(3, 0x1ef);
            this.btnluu.Name = "btnluu";
            this.btnluu.Size = new Size(0x4e, 0x17);
            this.btnluu.TabIndex = 0x15;
            this.btnluu.Text = " &Lưu [F5]";
            this.btnluu.Click += new EventHandler(this.btnluu_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.btnluu);
            base.Controls.Add(this.btnketthuc);
            base.Controls.Add(this.treeList1);
            this.LookAndFeel.UseWindowsXPTheme = true;
            base.Name = "UCphanquyensudung";
            base.Size = new Size(0x364, 0x209);
            base.Load += new EventHandler(this.UCphanquyensudung_Load);
            base.KeyDown += new KeyEventHandler(this.UCphanquyensudung_KeyDown);
            this.treeList1.EndInit();
            this.repositoryItemCheckEdit1.EndInit();
            base.ResumeLayout(false);
        }

        public virtual void stbnketthuc_Click(object sender, EventArgs e)
        {
            XtraTabPage parent = (XtraTabPage) base.Parent;
            XtraTabControl control = (XtraTabControl) parent.Parent;
            control.TabPages.Remove(parent);
        }

        private void treeList1_AfterCheckNode(object sender, NodeEventArgs e)
        {
            if (e.Node.HasChildren)
            {
                this.f_setNodeChild_check(e.Node);
            }
        }

        private void UCphanquyensudung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                this.btnluu_Click(null, null);
            }
            else if (e.KeyCode == Keys.F12)
            {
                this.btnketthuc_Click(null, null);
            }
        }

        private void UCphanquyensudung_Load(object sender, EventArgs e)
        {
            if (this._condata == null)
            {
                this._condata = new ConnectDatabase();
            }
            if (this._dataChucNang != null)
            {
                TreeListNode parentNode = this.treeList1.AppendNode(new object[] { "Tất cả", 0, 0 }, (TreeListNode) null);
                string str = new dtoDSThongSo(this._condata).TVQuyenSuDung(1);
                foreach (DataRow row in this._dataChucNang.Select("cap=1"))
                {
                    TreeListNode node2 = this.treeList1.AppendNode(new object[] { row["ten"].ToString(), row["ma"].ToString(), row["cap"].ToString() }, parentNode);
                    node2.SetValue("ma", row["ma"].ToString());
                    node2.Checked = str.IndexOf(node2.GetValue("ma").ToString()) > -1;
                    foreach (DataRow row2 in this._dataChucNang.Select("cap=2 and ma like '" + row["ma"].ToString() + "%'"))
                    {
                        TreeListNode node3 = this.treeList1.AppendNode(new object[] { row2["ten"].ToString(), row2["ma"].ToString(), row2["cap"].ToString() }, node2);
                        node3.SetValue("ma", row2["ma"].ToString());
                        node3.Checked = str.IndexOf(node3.GetValue("ma").ToString()) > -1;
                        foreach (DataRow row3 in this._dataChucNang.Select("cap=3 and ma like'" + row2["ma"].ToString() + "%'"))
                        {
                            TreeListNode node4 = this.treeList1.AppendNode(new object[] { row3["ten"].ToString(), row3["ma"].ToString(), row3["cap"].ToString() }, node3);
                            node4.SetValue("ma", row3["ma"].ToString());
                            node4.Checked = str.IndexOf(node4.GetValue("ma").ToString()) > -1;
                            foreach (DataRow row4 in this._dataChucNang.Select("cap=4 and ma like'" + row3["ma"].ToString() + "%'"))
                            {
                                TreeListNode node5 = this.treeList1.AppendNode(new object[] { row4["ten"].ToString(), row4["ma"].ToString(), row4["cap"].ToString() }, node4);
                                node5.SetValue("ma", row4["ma"].ToString());
                                node5.Checked = str.IndexOf(node5.GetValue("ma").ToString()) > -1;
                            }
                        }
                    }
                }
                this.treeList1.ExpandAll();
                this.treeList1.Refresh();
            }
        }

        public DataTable pChucNang
        {
            get
            {
                return this._dataChucNang;
            }
            set
            {
                this._dataChucNang = value;
            }
        }

        public ConnectDatabase pCondata
        {
            get
            {
                return this._condata;
            }
            set
            {
                this._condata = value;
            }
        }
    }
}

